package io.renren.modules.evaluate.controller;

import java.text.ParseException;
import java.util.Arrays;
import java.util.Map;
import java.util.List;

import io.renren.modules.evaluate.entity.NormMesEntity;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.renren.modules.evaluate.service.IndexMesService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import springfox.documentation.annotations.ApiIgnore;
import java.util.Date;
import java.text.SimpleDateFormat;
import io.renren.modules.evaluate.util.PingyinTest;

@RestController
@RequestMapping("generator/indexMes")
public class IndexMesController {
    @Autowired
    private IndexMesService indexMesService;

    /**
     * 分页展示列表
     */
    @ApiOperation("fenye")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", dataType = "int", value="1"),
            @ApiImplicitParam(paramType = "query",name="page", dataType = "int", value = "2"),
            @ApiImplicitParam(paramType = "query",name="limit", dataType = "int", value = "2")
    })
//    @RequestMapping("/list")
    @GetMapping("/list")
//    @RequiresPermissions("generator:normmes:list")
    public R list(@ApiIgnore @RequestParam(required = false) Map<String, Object> params){
        Object idObject = params.get("id");
        if(idObject != null){
            int id = Integer.parseInt(idObject.toString());
            NormMesEntity indexMes = indexMesService.getById(id);
            if(indexMes == null){
                return R.error(-1, "指标不存在");
            }
            return R.ok().put("normMes", indexMes);
        }else {
            PageUtils page = indexMesService.queryPage(params);
            return R.ok().put("page", page);
        }

    }


    /**
     * 信息
     */
    @ApiOperation("findById")
    @GetMapping("/info/{id}")
//    @RequiresPermissions("generator:normmes:info")
    public R info(@PathVariable("id") Integer id){
        NormMesEntity indexMes = indexMesService.getById(id);

        return R.ok().put("indexMes", indexMes);
    }

    /**
     * 模糊查询指标名
     */
    @ApiOperation("mohuQuery")
    @GetMapping("/getinfo")
//    @RequiresPermissions("generator:normmes:info")
    public R getInfo(@RequestParam String normName){

        List<NormMesEntity> indexMes = indexMesService.getByNormName(normName);

        return R.ok().put("indexMes", indexMes);
    }



    /**
     * 新增指标
     */
    @ApiOperation("indexAdd")
    @PostMapping("/add")
//    @RequiresPermissions("generator:normmes:save")
    public R save(@RequestBody NormMesEntity indexMes) throws ParseException {
        // 首先查看数据库中是否存在该指标
        String indexName = indexMes.getMes();
        NormMesEntity indexmes = indexMesService.queryByIndexName(indexName);
        // 如果存在，则返回-1
        if(indexmes != null){
            return R.error(-1,"添加的指标已经存在");
        }
        // 时间转换
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");
        Date now = new Date();
        String resultDate = sdf.format(now);
        Date last = sdf.parse(resultDate);
        // 中文转英文
        String engNormName = PingyinTest.getAllPinyin(indexName);
        indexMes.setNormName(engNormName);
        indexMes.setId(0);
        indexMes.setProperty("0");
        indexMes.setTime(last);
        indexMes.setFrom_table_id(0);
        indexMes.setStatus("0");
        // 如果不存在，则更新数据库
        indexMesService.save(indexMes);
        return R.ok();
    }

    /**
     * 修改
     */
    @ApiOperation("indexReverse")
    @PutMapping("/update")
//    @RequiresPermissions("generator:normmes:update")
    public R update(@RequestBody NormMesEntity indexMes){

        indexMesService.updateById(indexMes);
        return R.ok();
    }

    /**
     * 根据 id 删除指标信息
     */
    @ApiOperation("indexDelete")
    @GetMapping("/delete")
//    @RequiresPermissions("generator:normmes:delete")
    public R delete(@RequestParam Integer[] ids){
        indexMesService.logicalDelete(Arrays.asList(ids));
        return R.ok();
    }

}
